
package com.hjb.service.config.web;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * @ClassName: JsonToMapper
 * @author <a href="mailto:panxiwen@yingu.com">Pan Xinwen</a>
 * @date 2016年9月18日 下午4:33:46
 */
@Configuration
public class JsonToMapper {

    //数据格式化
    @Bean
    public ObjectMapper objectMapper() {
    	ObjectMapper objectMapper = new ObjectMapper();
    	// 空值处理为空串
    	objectMapper.getSerializerProvider().setNullValueSerializer(
				new JsonSerializer<Object>() {
					@Override
					public void serialize(Object value, JsonGenerator jg,
							SerializerProvider sp) throws IOException,
                            JsonProcessingException {
						jg.writeString("");
					}
				});
    	
    	objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    	return objectMapper;  
    } 
}

